package com.imooc.service.impl;

import com.imooc.common.dto.OrderDTO;
import com.imooc.service.PushMessage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class PushMessageImpl implements PushMessage {

    @Resource
    WxMpService wxMpService;

    @Override
    public void orderStatus(OrderDTO orderDTO) {
        try {
            WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
            wxMpTemplateMessage.setTemplateId("W0rpQITY9mOb14PZyZQ8lr4MfIRqqlnnCfwMgH436kc");
            wxMpTemplateMessage.setToUser(orderDTO.getBuyerOpenid());
            List<WxMpTemplateData> templateDataList = Arrays.asList(
                    new WxMpTemplateData("first", "亲，请记得收货"),
                    new WxMpTemplateData("keyword1", orderDTO.getBuyerName()),
                    new WxMpTemplateData("keyword2", orderDTO.getBuyerPhone()),
                    new WxMpTemplateData("keyword3", orderDTO.getOrderId()),
                    new WxMpTemplateData("keyword4", orderDTO.getOrderStatus().toString()),
                    new WxMpTemplateData("keyword5", orderDTO.getOrderAmount().toPlainString()),
                    new WxMpTemplateData("remark", "亲，记得点个赞哦")
            );
            wxMpTemplateMessage.setData(templateDataList);

            String templateMsg = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);

        } catch (WxErrorException e) {
            log.error("微信发送模板消息失败");
        }
    }
}
